//
//  AddFamilyViewController.m
//  workApp
//
//  Created by 林 on 2019/1/13.
//  Copyright © 2019 林. All rights reserved.
//

#import "AddFamilyViewController.h"
#import "PGDatePickManager.h"
@interface AddFamilyViewController ()<UITableViewDelegate,UITableViewDataSource,PGDatePickerDelegate>

@property (nonatomic,strong)UITextField * nameText;
@property (nonatomic,strong)UIButton * selectBtn;
@property (nonatomic,strong)UILabel * birthLab;
@property (nonatomic,copy)NSString * sexStr;
@property (nonatomic,copy)NSString * year;
@property (nonatomic,copy)NSString * mouth;
@property (nonatomic,strong)UITextField * relationLab;
@property (nonatomic,strong)UITextField * phoneText;
@end

@implementation AddFamilyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _sexStr = @"男";
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"家庭成员信息";
    UITableView * tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, MDSCREEN_WIDTH, MDSCREEN_HEIGHT - 100) style:(UITableViewStylePlain)];
    tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableV.delegate = self;
    tableV.dataSource = self;
    tableV.showsHorizontalScrollIndicator = false;
    tableV.showsVerticalScrollIndicator = false;
    [self.view addSubview:tableV];
    
    UIButton * newBoxBtn = [[UIButton alloc]initWithFrame:CGRectMake(100, MDSCREEN_HEIGHT - 80, MDSCREEN_WIDTH - 200, 50)];
    newBoxBtn.backgroundColor = [UIColor colorWithHexStr:@"#6fac45"];
    [newBoxBtn setTitle:@"保存" forState:(UIControlStateNormal)];
    newBoxBtn.layer.cornerRadius = 25;
    newBoxBtn.layer.masksToBounds = true;
    [newBoxBtn addTarget:self action:@selector(addNewClick) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:newBoxBtn];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 70;
    }
    
    return 55;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellOne"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cellOne"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor = [UIColor whiteColor];
            UIView * backView = [[UIView alloc]initWithFrame:CGRectMake(30, 20, MDSCREEN_WIDTH - 60, 50)];
            backView.layer.cornerRadius = 5;
            backView.layer.masksToBounds = true;
            backView.layer.borderWidth = 1;
            backView.layer.borderColor = [UIColor colorWithHexStr:@"#dfdfdf"].CGColor;
            backView.backgroundColor = [UIColor colorWithHexStr:@"#f4f4f4"];
            [cell addSubview:backView];
            
            self.nameText = [[UITextField alloc]initWithFrame:CGRectMake(10, 15, 100, 20)];
            self.nameText.placeholder = @"请输入姓名";

            [backView addSubview:self.nameText];
            
            UIView * sexBackView = [[UIView alloc]initWithFrame:CGRectMake(MDSCREEN_WIDTH - 180, 10, 100, 30)];
            sexBackView.backgroundColor = [UIColor whiteColor];
            sexBackView.layer.masksToBounds = true;
            sexBackView.layer.cornerRadius = 15;
            [backView addSubview:sexBackView];
            
            for (int i=0; i<2; i++) {
                UIButton * sexBtn = [[UIButton alloc]initWithFrame:CGRectMake(50*i, 0, 50, 30)];
                sexBtn.layer.cornerRadius = 15;
                sexBtn.layer.masksToBounds = true;
                [sexBtn setTitleColor:[UIColor colorWithHexStr:@"#969696"] forState:(UIControlStateNormal)];
                sexBtn.tag = 15+i;
                [sexBtn addTarget:self action:@selector(sexClick:) forControlEvents:(UIControlEventTouchUpInside)];
                if (i==0) {
                    self.selectBtn = sexBtn;
//                                        self.selectBtn.backgroundColor = [UIColor whiteColor];
                    [sexBtn setTitle:@"男" forState:(UIControlStateNormal)];
                    sexBtn.backgroundColor = [UIColor colorWithHexStr:@"#ed7d31"];
                    [sexBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
                
//
                }
                else
                {
                    [sexBtn setTitle:@"女" forState:(UIControlStateNormal)];
                }
                [sexBackView addSubview:sexBtn];
            }
            
        }

        return cell;
    }
    else if (indexPath.row == 1)
    {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellTwo"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cellTwo"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor = [UIColor whiteColor];
            
            UILabel * birthTitleLab = [[UILabel alloc]initWithFrame:CGRectMake(50, 15, 80, 40)];
            birthTitleLab.textAlignment = NSTextAlignmentRight;
            birthTitleLab.text = @"出生年月";
            [cell addSubview:birthTitleLab];
            
            UIView * birthBackView = [[UIView alloc]initWithFrame:CGRectMake(150, 15, MDSCREEN_WIDTH - 180, 40)];
            birthBackView.layer.cornerRadius = 5;
            birthBackView.layer.masksToBounds = true;
            birthBackView.layer.borderWidth = 1;
            birthBackView.layer.borderColor = [UIColor colorWithHexStr:@"#dfdfdf"].CGColor;
            birthBackView.backgroundColor = [UIColor colorWithHexStr:@"#f4f4f4"];
            [cell addSubview:birthBackView];
            
            self.birthLab = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 80, 20)];

            self.birthLab.font = [UIFont systemFontOfSize:15];
            [birthBackView addSubview:self.birthLab];
            
            UIImageView * imageV = [[UIImageView alloc]initWithFrame:CGRectMake(birthBackView.frame.size.width - 30, 10, 20, 20)];

            imageV.image = [UIImage imageNamed:@"rili"];
            [birthBackView addSubview:imageV];
            
            UIButton * birthBtn = [[UIButton alloc]initWithFrame:CGRectMake(150, 15, MDSCREEN_WIDTH - 180, 40)];
            [birthBtn addTarget:self action:@selector(birthClick) forControlEvents:(UIControlEventTouchUpInside)];
            [cell addSubview:birthBtn];
        }
        
        return cell;
    }
    else if (indexPath.row == 2)
    {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellThree"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cellThree"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor = [UIColor whiteColor];
            
            UILabel * birthTitleLab = [[UILabel alloc]initWithFrame:CGRectMake(50, 15, 80, 40)];
            birthTitleLab.textAlignment = NSTextAlignmentRight;
            birthTitleLab.text = @"关系";
            [cell addSubview:birthTitleLab];
            
            UIView * birthBackView = [[UIView alloc]initWithFrame:CGRectMake(150, 15, MDSCREEN_WIDTH - 180, 40)];
            birthBackView.layer.cornerRadius = 5;
            birthBackView.layer.masksToBounds = true;
            birthBackView.layer.borderWidth = 1;
            birthBackView.layer.borderColor = [UIColor colorWithHexStr:@"#dfdfdf"].CGColor;
            birthBackView.backgroundColor = [UIColor colorWithHexStr:@"#f4f4f4"];
            [cell addSubview:birthBackView];
            
            self.relationLab = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 80, 20)];
            self.relationLab.placeholder = @"请输入关系";
//            self.relationLab.text = @"岳父";
//            relationLab.backgroundColor = [UIColor redColor];
            self.relationLab.font = [UIFont systemFontOfSize:15];
            [birthBackView addSubview:self.relationLab];
            
            UIImageView * imageV = [[UIImageView alloc]initWithFrame:CGRectMake(birthBackView.frame.size.width - 30, 10, 20, 20)];
//            imageV.backgroundColor = [UIColor redColor];
            [birthBackView addSubview:imageV];
            
            
        }
        
        return cell;
    }
    else if(indexPath.row == 3)
    {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellFour"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cellFour"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor = [UIColor whiteColor];
            
            UILabel * birthTitleLab = [[UILabel alloc]initWithFrame:CGRectMake(50, 15, 80, 40)];
            birthTitleLab.textAlignment = NSTextAlignmentRight;
            birthTitleLab.text = @"手机号";
            [cell addSubview:birthTitleLab];
            
            UIView * birthBackView = [[UIView alloc]initWithFrame:CGRectMake(150, 15, MDSCREEN_WIDTH - 180, 40)];
            birthBackView.layer.cornerRadius = 5;
            birthBackView.layer.masksToBounds = true;
            birthBackView.layer.borderWidth = 1;
            birthBackView.layer.borderColor = [UIColor colorWithHexStr:@"#999999"].CGColor;
            birthBackView.backgroundColor = [UIColor colorWithHexStr:@"#ffffff"];
            [cell addSubview:birthBackView];
            
            self.phoneText = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, MDSCREEN_WIDTH - 210, 20)];
            self.phoneText.placeholder = @"请输入电话号码";
//            self.phoneText.text = @"";
            self.phoneText.textColor = [UIColor colorWithHexStr:@"#333333"];
            self.phoneText.keyboardType = UIKeyboardTypeNumberPad;
            self.phoneText.backgroundColor = [UIColor whiteColor];
            self.phoneText.font = [UIFont systemFontOfSize:15];
            [birthBackView addSubview:self.phoneText];
        }
        
        return cell;
    }
    else if(indexPath.row == 4)
    {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellFive"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cellFive"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor = [UIColor whiteColor];
            
            UIButton * birthBackView = [[UIButton alloc]initWithFrame:CGRectMake(150, 10, 20, 20)];
            [birthBackView addTarget:self action:@selector(chiose:) forControlEvents:(UIControlEventTouchUpInside)]; birthBackView.layer.cornerRadius = 10;
            birthBackView.layer.masksToBounds = true;
            birthBackView.layer.borderWidth = 1;
            birthBackView.layer.borderColor = [UIColor colorWithHexStr:@"#dcdcdc"].CGColor;
            birthBackView.backgroundColor = [UIColor colorWithHexStr:@"#ffffff"];
             [birthBackView setBackgroundImage:[UIImage imageNamed:@""] forState:(UIControlStateNormal)];
            [birthBackView setBackgroundImage:[UIImage imageNamed:@"1.jpg"] forState:(UIControlStateSelected)];
            [cell addSubview:birthBackView];
            
            UILabel * relationLab = [[UILabel alloc]initWithFrame:CGRectMake(190, 10, 100, 20)];
            relationLab.text = @"联系我就好";
           
            relationLab.textColor = [UIColor colorWithHexStr:@"#333333"];
            relationLab.backgroundColor = [UIColor whiteColor];
            relationLab.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:relationLab];
        }
        
        return cell;
    }
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        cell.backgroundColor = [UIColor redColor];
    }
    cell.textLabel.text = @"123";
    return cell;
}
- (void)sexClick:(UIButton *)btn
{
    self.selectBtn.backgroundColor = [UIColor whiteColor];
    [self.selectBtn setTitleColor:[UIColor colorWithHexStr:@"#969696"] forState:(UIControlStateNormal)];
    self.selectBtn = btn ;
    btn.backgroundColor = [UIColor colorWithHexStr:@"#ed7d31"];
  
    [btn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    if (btn.tag == 15) {
        self.sexStr = @"男";
    }
    else
    {
        self.sexStr = @"女";
    }
}
- (void)birthClick{
    PGDatePickManager *datePickManager = [[PGDatePickManager alloc]init];
    datePickManager.style = PGDatePickManagerStyleAlertBottomButton;
    datePickManager.isShadeBackground = true;
    PGDatePicker *datePicker = datePickManager.datePicker;
    datePicker.isHiddenMiddleText = false;
    datePicker.delegate = self;
    datePicker.datePickerType = PGDatePickerType2;
    datePicker.datePickerMode = PGDatePickerModeYearAndMonth;
    datePicker.maximumDate = [NSDate date];
    [self presentViewController:datePickManager animated:false completion:nil];
}
- (void)chiose:(UIButton *)btn{
    btn.selected = !btn.selected;
    self.phoneText.text = FamilyPhone;
}
- (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents {
    NSLog(@"dateComponents = %@", dateComponents);
    self.birthLab.text = [NSString stringWithFormat:@"%ld年%ld月",(long)dateComponents.year,(long)dateComponents.month];
   
    self.year = [NSString stringWithFormat:@"%ld",dateComponents.year];
    self.mouth = [NSString stringWithFormat:@"%ld",dateComponents.month];
}
- (void)addNewClick{
    if (self.nameText.text.length <= 0) {
        [MBProgressHUD showInfoMessage:@"请输入姓名"];
        return;
    }
    else if (self.birthLab.text.length <= 0)
    {
        [MBProgressHUD showInfoMessage:@"请选择出生年月"];
        return;
    }
    else if (self.relationLab.text.length <= 0)
    {
        [MBProgressHUD showInfoMessage:@"请输入关系"];
        return;
    }
    else if (self.relationLab.text.length <= 0)
    {
        [MBProgressHUD showInfoMessage:@"请输入手机号码"];
        return;
    }
    NSDictionary * dic = @{@"family_id":FamilyID,@"name":self.nameText.text,@"sex":self.sexStr,@"birth_year":self.year,@"birth_month":self.mouth,@"relationship":self.relationLab.text,@"phone":self.phoneText.text,@"family_phone":FamilyPhone};
    
    [MDNetDetail POSTNet:AddFamilyMenber_url param:dic block:^(id posts, NSInteger code, NSString *Meg) {
        [MBProgressHUD showInfoMessage:Meg];
        if ([Meg isEqualToString:@"添加成功"]) {
            [self.navigationController popViewControllerAnimated:true];
        }
        
        
    }];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
